var pattern = new RegExp(/\?Code=(\S*)/g);
var idx = pattern.exec(window.location.href);
var _Data = [], Data02 = [], Data03=[], _Level;

var grid02 = $('#jgTable02'), grid03 = $('#jgTable03');

$(function(){
    requestAjax();
    loadAjax();
    requestAddress(0,1);
    $("li[data-opt='back']").on('click', back);
    $('#ItemProvince').on('change', function(){
        var PCode = $(this).find('option:selected').attr('data-Code');
        requestAddress(PCode,2);
        $('#ItemCity').trigger('change');
    });
    $('#ItemCity').on('change', function(){
        var PCode = $(this).find('option:selected').attr('data-Code');
        requestAddress(PCode,3);
        $('#ItemArea').trigger('change');
    });
    $('#ItemArea').on('change', function(){
        var PCode = $(this).find('option:selected').attr('data-Code');
        requestAddress(PCode,4);
    });
    $('#ItemProvince').trigger('change');
    $('i.fa-paste').on('click', function(){
        $('#ItemCreditCode').val($('#ItemDutyNo').val());
    });
    $('#item02_add').on('click', function(){    //MDM001404-添加联系人
        var domarr = $('li[data-tab="MDM001401"]').find('input');
        var status = true;
        for(var i=0;i<domarr.length;i++){
            domarr.eq(i).val()==''&&(status = false);
        }
        // if(status){
            $('#add02').removeClass('hidden');
            $('#m_UserCode').val('');
            $('#m_UserName').val('');
            $('#m_CellPhoneNo').val('');
            $('#m_PhoneNO').val('');
            $('#sbt02').unbind('click').bind('click', function(){
                if($('#m_UserName').val()!==''&&($('#m_CellPhoneNo').val()!==''||$('#m_PhoneNO').val()!=='')){
                    if($('#m_CellPhoneNo').val()!==''){
                        var status = /^1(3|4|5|7|8)[0-9]\d{8}$/.test($('#m_CellPhoneNo').val())
                    } else {
                        var status = true
                    }
                    if(status){
                        $('#add02').addClass('hidden');
                        var postData02 = {
                            Method: 'A',
                            Content:{
                                Code: idx[1],
                                UserCode: '',
                                UserName: $('#m_UserName').val(),
                                CellPhoneNO: $('#m_CellPhoneNo').val(),
                                PhoneNO: $('#m_PhoneNO').val()
                            }
                        };
                        for(i in postData){
                            postData02[i] = postData[i];
                        }
                        console.log(postData02)
                        $.ajax({
                            type: 'post',
                            url: mdmPath + '/MDM0014/MDM001404',
                            data: postData02,
                            dataType: 'json',
                            success: function(re) {
                                if(re.Status == 200){
                                    myAlert('保存成功');
                                    requestAjax();
                                    Data02 = _Data.Content.MDM001402;
                                    grid02.trigger('reloadGrid');
                                }else{
                                    myAlert(re.Message);
                                }
                            }
                        });
                    } else {
                        myAlert('请填写正确的手机号')
                    }
                } else {
                    myAlert('请填写完联系人信息');
                }
            });
        // }else{
        //     myAlert('请填写完开票信息');
        // }
    });
    $('#item03_add').on('click', function(){
        $('#add03').removeClass('hidden');
        var LineID = grid03.jqGrid('getRowData').length + 1;
        $('#ItemLineID').val(LineID);
        $('#sbt03').unbind('click').bind('click', function(){
            $('#add03').addClass('hidden');
            var postData03 = {
                Method: 'A',
                Content:{
                    Code: idx[1],
                    LineID: LineID,
                    Province: $('#ItemProvince').val(),
                    City: $('#ItemCity').val(),
                    Area: $('#ItemArea').val(),
                    Street: $('#ItemStreet').val(),
                    Address: $('#ItemAddress').val()
                }
            };
            console.log(postData03)
            for(i in postData){
                postData03[i] = postData[i];
            }
            $.ajax({
                type: 'post',
                url: mdmPath + '/MDM0014/MDM001405',
                data: postData03,
                dataType: 'json',
                success: function(re) {
                    if(re.Status == 200){
                        myAlert('保存成功');
                        requestAjax();
                        Data03 = _Data.Content.MDM001403;
                        grid03.trigger('reloadGrid');
                    }else{
                        myAlert(re.Message);
                    }
                }
            });
        });
    });
    $('li[data-opt="u_save"]').on('click', saveSubmit);
    $('.modal-data').draggable();
});

function requestAddress(PCode, Level){
    _Level = Level;
    var _postData = Object.create(postData);
    _postData.Method = 'Q';
    _postData.Content = {
        PCode: PCode
    };
    $.ajax({
        async: false,
        type: 'post',
        url: mdmPath + '/MDM0034/MDM003404',
        data: _postData,
        dataType: 'json',
        success: function (re) {
            if (re.Status == 200) {
                loadAddress(re);
            } else {
                alert(re.Message);
            }
        }
    });
}
function loadAddress(re){
    var arr = re.Content.loMDM0034RedisReturnList,
        options = '';
    arr.forEach(function(v,i,a){
        options += '<option value="'+v.Name+'" data-Code="'+v.Code+'">'+v.Name+'</option>'
    });
    switch(_Level){
        case 1:
            $('#ItemProvince').html(options);
        break;
        case 2:
            $('#ItemCity').html(options);
        break;
        case 3:
            $('#ItemArea').html(options);
        break;
        case 4:
            $('#ItemStreet').html(options);
        break;
    }
}

function requestAjax(){
    if (idx) {
        var _postData = Object.create(postData);    //请求头
        _postData.Method = 'Q';
        _postData.Content = {
            Code: idx[1]
        }
        $.ajax({
            async: false,
            type: 'post',
            url: mdmPath + '/MDM0014/MDM001403',
            data: _postData,
            dataType: 'json',
            success: function (re) {
                if (re.Status == 200) {
                    _Data = re;
                } else if (re.Status == -999) {
                    alert_QX($('body'), re.Message);
                }  else {
                    alert(re.Message);
                }
            }
        });
    }
}
function loadAjax(){
    re = _Data;
    $('#ItemCode').val(re.Content.Code);
    $('#ItemName').val(re.Content.Name);
    $('#ItemStatus').val(re.Content.Status);
    $('#ItemCardType').val(re.Content.CardType);
    $('#ItemCusSource').val(re.Content.CusSource);
    $('#ItemCS').val(re.Content.CS);
    $('#ItemSaler').val(re.Content.Saler);
    $('#ItemSalerManager').val(re.Content.SalerManager);
    $('#ItemPayMent').val(re.Content.PayMent);
    $('#ItemSalerPhone').val(re.Content.SalerPhone);
    $('#ItemCompanyName').val(re.Content.CompanyName);
    $('#ItemOpeningBank').val(re.Content.OpeningBank);
    $('#ItemLPhone').val(re.Content.LPhone);
    $('#ItemLaddress').val(re.Content.Laddress);
    $('#ItemBankAccount').val(re.Content.BankAccount);
    $('#ItemDutyNo').val(re.Content.DutyNo);
    $('#ItemCreditCode').val(re.Content.CreditCode);
    Data02 = re.Content.MDM001402;
    Data03 = re.Content.MDM001403;
    //console.log(Data02);
    //console.log(Data03)
    gridInit02();
    gridInit03();
}

function gridInit02(){
    var gridData02 = Object.create(gridData);
    gridData02.grid = $('#jgTable02');
    gridData02.datatype = "local";
    gridData02.colModel = [
        {label:'行号', name:'LineID', sortable: false},
        {label:'代码', name:'UserCode', sortable: false},
        {label:'姓名', name:'UserName', sortable: false},
        {label:'手机', name:'CellPhoneNO', sortable: false},
        {label:'座机', name:'PhoneNO', sortable: false}
    ];
    gridData02.rowNum = '100000';
    gridData02.beforeRequest = function(){
        gridData02.grid.jqGrid("setGridParam", {
            data : Data02,
        });
    };
    gridData02.loadComplete = function(xhr){
        gridSize()
    }
    gridData02.ondblClickRow = function(rowid,iRow,iCol,e){
        dbEdit02(rowid);
    };
    gridInit(gridData02);
}

function gridInit03(){
    var gridData03 = Object.create(gridData);
    gridData03.grid = $('#jgTable03');
    gridData03.datatype = "local";
    gridData03.colModel = [
        {label:'省', name:'Province', sortable: false},
        {label:'市', name:'City', sortable: false},
        {label:'区', name:'Area', sortable: false},
        {label:'街道', name:'Street', sortable: false},
        {label:'详细地址', name:'Address', sortable: false},
    ];
    gridData03.rowNum = '100000';
    gridData03.beforeRequest = function(){
        gridData03.grid.jqGrid("setGridParam", {
            data : Data03,
        });
    };
    gridData03.loadComplete = function(xhr){
        gridSize()
    }
    gridData03.ondblClickRow = function(rowid,iRow,iCol,e){
        dbEdit03(rowid);
    };
    gridInit(gridData03);
}

//操作
function back(){
    window.open('MDM001401.html', '_self');
}
function dbEdit02(rowid){   //修改联系人
    $('#add02').removeClass('hidden');
    var row02 = grid02.jqGrid('getRowData', rowid);
    $('#m_UserCode').val(row02.UserCode);
    $('#m_UserName').val(row02.UserName);
    $('#m_CellPhoneNo').val(row02.CellPhoneNO);
    $('#m_PhoneNO').val(row02.PhoneNO);
    $('#sbt02').unbind('click').bind('click', function(){
        if($('#m_UserName').val()!==''&&($('#m_CellPhoneNo').val()!==''||$('#m_PhoneNO').val()!=='')){
            if($('#m_CellPhoneNo').val()!==''){
                var status = /^1(3|4|5|7|8)[0-9]\d{8}$/.test($('#m_CellPhoneNo').val())
            } else {
                var status = true
            }
            if(status){
                $('#add02').addClass('hidden');
                var postData02 = {
                    Method: 'U',
                    Content:{
                        Code: idx[1],
                        LineID: row02.LineID,
                        UserCode: $('#m_UserCode').val(),
                        UserName: $('#m_UserName').val(),
                        CellPhoneNO: $('#m_CellPhoneNo').val(),
                        PhoneNO: $('#m_PhoneNO').val()
                    }
                };
                for(i in postData){
                    postData02[i] = postData[i];
                }
                console.log(postData02)
                $.ajax({
                    type: 'post',
                    url: mdmPath + '/MDM0014/MDM001404',
                    data: postData02,
                    dataType: 'json',
                    success: function(re) {
                        if(re.Status == 200){
                            myAlert('修改成功');
                            requestAjax();
                            Data02 = _Data.Content.MDM001402;
                            grid02.trigger('reloadGrid');
                        }else{
                            myAlert(re.Message);
                        }
                    }
                });
            } else {
                myAlert('请填写正确的手机号')
            }
        } else {
            myAlert('请填写完联系人信息');
        }
    });
}
function dbEdit03(rowid){   //修改地址
    $('#add03').removeClass('hidden');
    $('#ItemLineID').val(rowid);
    $('#sbt03').unbind('click').bind('click', function(){
        $('#add03').addClass('hidden');    
        var postData03 = {
            Method: 'U',
            Content:{
                Code: idx[1],
                LineID: rowid,
                Province: $('#ItemProvince').val(),
                City: $('#ItemCity').val(),
                Area: $('#ItemArea').val(),
                Street: $('#ItemStreet').val(),
                Address: $('#ItemAddress').val()
            }
        };
        for(i in postData){
            postData03[i] = postData[i];
        }
        console.log(postData03)
        $.ajax({
            type: 'post',
            url: mdmPath + '/MDM0014/MDM001405',
            data: postData03,
            dataType: 'json',
            success: function(re) {
                if(re.Status == 200){
                    myAlert('保存成功');
                    requestAjax();
                    Data03 = _Data.Content.MDM001403;
                    grid03.trigger('reloadGrid');
                }else{
                    myAlert(re.Message);
                }
            }
        });
    });
}

function saveSubmit(){
    var _postData = Object.create(postData);
    _postData.Content = {
        Code: $('#ItemCode').val(),
        Name: $('#ItemName').val(),
        Status: $('#ItemStatus').val(),
        CardType: $('#ItemCardType').val(),
        CusSource: $('#ItemCusSource').val(),
        CS: $('#ItemCS').val(),
        Saler: $('#ItemSaler').val(),
        SalerManager: $('#ItemSalerManager').val(),
        PayMent: $('#ItemPayMent').val(),
        SalerPhone: $('#ItemSalerPhone').val(),
        CompanyName: $('#ItemCompanyName').val(),
        OpeningBank: $('#ItemOpeningBank').val(),
        LPhone: $('#ItemLPhone').val(),
        Laddress: $('#ItemLaddress').val(),
        BankAccount: $('#ItemBankAccount').val(),
        DutyNo: $('#ItemDutyNo').val(),
        CreditCode: $('#ItemCreditCode').val()
    }
    if(idx){
        _postData.Method = 'U';        
    }else{
        _postData.Method = 'A';
        //_postData.Content.Code = '0';
    }
    console.log(_postData);
    $.ajax({
        type: 'post',
        url: mdmPath + '/MDM0014/MDM001401',
        data: _postData,
        dataType: 'json',
        success: function(re) {
            if(re.Status == 200){
                myAlert('保存成功');
                // console.log(re.Content.Code)
                // window.open('MDM001402.html?Code=' + re.Content.Code, '_self');
            }else{
                myAlert(re.Message);                
            }
        }
    });
}

//jqGrid样式
function gridSize(){
    grid02.setGridWidth($('.u_tabs_container').width() - 30);
    grid03.setGridWidth($('.u_tabs_container').width() - 30);
}